"""
URL configuration for tutorial project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include, re_path
from django.http import HttpResponse, JsonResponse
from django.contrib.auth.models import User
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token

from quickstart import views
from rest_framework import routers
from book import views as bookviews

from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework import status

from rest_framework.urlpatterns import format_suffix_patterns

router = routers.DefaultRouter()
# router = routers.SimpleRouter()
# 添加子路由（注册用户与组资源）
# 使用模型视图集 会生成对应的两种路由 第一种列表路由 basename-list 第二种详情路由 basename-detail basename 默认就是模型名
# router.urls 可以返回列表格式的路由模式

# router.register(r'users', views.UserGenericViewSet, basename='user')
router.register(r'groups', views.GroupGenericViewSet, basename='group')

router.register(r'books', bookviews.BookGenericViewSet, basename='book')
router.register(r'categorys', bookviews.CategoryGenericViewSet, basename='category')


def compare_fun(req):
    # Content-Type: text/html; charset=utf-8
    # return HttpResponse("<h1>我是html内容</h1>")

    # Content-Type: application/json
    # return JsonResponse({
    #     "code": 0,
    #     "msg": "我是Json内容"
    # })
    users = User.objects.all()
    # 因为本身Django的查询集序列化不方便 所以需要引入drf
    users = [{"name": u.username} for u in users]
    # print(users)
    return JsonResponse({
        "code": 0,
        "msg": "获取用户列表成功",
        "datas": users,
    })


@api_view(["GET"])
def api_root(req):
    return Response(data={
        "users": reverse("user-list", request=req),
        "groups": reverse("group-list", request=req),
        "books": reverse("book-list", request=req),
        "categorys": reverse("category-list", request=req),
    }, status=status.HTTP_200_OK)


urlpatterns = [
    path("admin/", admin.site.urls),

    path("compare/", compare_fun),
    path('', include(router.urls)),
    # 能够使用drf 自带的登录和退出
    path('', include("rest_framework.urls")),

    # path('books/', bookviews.book_list, name="book-list"),
    # path('books/<int:pk>/', bookviews.book_detail, name="book-detail"),
    # path('categorys/', bookviews.category_list, name='category-list'),
    # path('categorys/<int:pk>/', bookviews.category_detail, name='category-detail'),

    # re_path(r'^users/$', views.UserListView.as_view(), name="user-list"),
    # re_path(r'^users/(?P<pk>[^/.]+)/$', views.UserDetailView.as_view(), name="user-detail"),
    # re_path(r'^groups/$', views.GroupListView.as_view(), name="group-list"),
    # re_path(r'^groups/(?P<pk>[^/.]+)/$', views.GroupDetailView.as_view(), name="group-detail"),

    # re_path(r'^users/$', views.UserListCreateGenericView.as_view(), name="user-list"),
    # re_path(r'^users/(?P<pk>[^/.]+)/$', views.UserDetailRetrieveUpdateDestroyGenericView.as_view(), name="user-detail"),
    # re_path(r'^groups/$', views.GroupListCreateGenericView.as_view(), name="group-list"),
    # re_path(r'^groups/(?P<pk>[^/.]+)/$', views.GroupDetailRetrieveUpdateDestroyGenericView.as_view(),
    #         name="group-detail"),

    re_path(r'^users/$', views.UserGenericViewSet.as_view({'get': 'list', 'post': 'create'}), name="user-list"),
    re_path(r'^update_password/$', views.update_password, name="update_password"),
    re_path(r'^get_user_info/$', views.get_user_info, name="get_user_info"),

    # re_path(r'^users/(?P<pk>[^/.]+)/$',
    #         views.UserGenericViewSet.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}),
    #         name="user-detail"),
    # re_path(r'^groups/$', views.GroupGenericViewSet.as_view({'get': 'list', 'post': 'create'}), name="group-list"),
    # re_path(r'^groups/(?P<pk>[^/.]+)/$', views.GroupGenericViewSet.as_view(
    #     {'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}),
    #         name="group-detail"),

    # re_path(r'^books/$', bookviews.BookListGenericView.as_view(), name="book-list"),
    # re_path(r'^books/(?P<pk>[^/.]+)/$', bookviews.BookDetailGenericView.as_view(), name="book-detail"),
    # re_path(r'^categorys/$', bookviews.CategoryListGenericView.as_view(), name="category-list"),
    # re_path(r'^categorys/(?P<pk>[^/.]+)/$', bookviews.CategoryDetailGenericView.as_view(), name="category-detail"),

    # re_path(r'^books/$', bookviews.BookListCreateGenericView.as_view(), name="book-list"),
    # re_path(r'^books/(?P<pk>[^/.]+)/$', bookviews.BookDetailRetrieveUpdateDestroyGenericView.as_view(),
    #         name="book-detail"),
    # re_path(r'^categorys/$', bookviews.CategoryListCreateGenericView.as_view(), name="category-list"),
    # re_path(r'^categorys/(?P<pk>[^/.]+)/$', bookviews.CategoryDetailRetrieveUpdateDestroyGenericView.as_view(),
    #         name="category-detail"),

    # re_path(r'^books/$', bookviews.BookGenericViewSet.as_view({'get': 'list', 'post': 'create'}), name="book-list"),
    # re_path(r'^books/(?P<pk>[^/.]+)/$',
    #         bookviews.BookGenericViewSet.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}),
    #         name="book-detail"),
    # re_path(r'^categorys/$', bookviews.CategoryGenericViewSet.as_view({'get': 'list', 'post': 'create'}),
    #         name="category-list"),
    # re_path(r'^categorys/(?P<pk>[^/.]+)/$',
    #         bookviews.CategoryGenericViewSet.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}),
    #         name="category-detail"),

    # path('', api_root, name="api-root")

    # jwt 路由
    re_path(r'^api-token-auth/', obtain_jwt_token),
    re_path(r'^api-token-refresh/', refresh_jwt_token),
]

# 生成带有format的路由
# urlpatterns = format_suffix_patterns(urlpatterns)
